﻿using System;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using OurSite.Business.Event;
using OurSite.Business.Repositories;


namespace OurSite.Tests
{
    [TestFixture]
    public class EventRepository_Fixture
    {
        Guid g = new Guid();

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            NHibernateSessionManager.Instance.Initialize();
        }

        [Test]
        public void Can_add_new_Event()
        {
            
            var evt = new Event 
                {   Name = "Apple2", 
                    StartDate = DateTime.Now, 
                    EndDate = DateTime.Now.AddDays(2),
                    Order = 999
                };
            IEventRepository repository = new EventRepository();
            repository.Add(evt);
        }

        [Test]
        public void Can_get_Event_by_Name()
        {
            IEventRepository repository = new EventRepository();
            var evt = repository.GetByName("Apple2");
            g = evt.Id;
            Assert.IsNotNull(evt);
            Assert.AreEqual("Apple2", evt.Name);
        }

        [Test]
        public void Can_get_Event_by_ID()
        {
            IEventRepository repository = new EventRepository();
            var evt = repository.GetById(new Guid("28D42D89-F1BD-4B02-A1C8-8D345F93E2C3"));
            Assert.IsNotNull(evt);
            //Assert.AreEqual("Apple2", evt.Name);
        }

        [Test]
        public void Can_update_Event()
        {
            IEventRepository repository = new EventRepository();
            var evt = repository.GetById(g);
            evt.Name = "Yellow Pear";
            repository.Update(evt);

            var fromDb = repository.GetById(evt.Id);
            Assert.AreEqual(evt.Name, fromDb.Name);            
        }

        [Test]
        public void Can_delete_Event()
        {
            IEventRepository repository = new EventRepository();
            var evt = repository.GetById(g);
            repository.Remove(evt);

            var fromDb = repository.GetById(evt.Id);
            Assert.IsNull(fromDb);
        }

        [Test]
        public void Can_get_Events_by_year()
        {
            IEventRepository repository = new EventRepository();
            var fromDb = repository.GetByYear(2010);

            Assert.Greater(fromDb.Count, 10);
        }

        [Test]
        public void Can_get_Event_years()
        {
            IEventRepository repository = new EventRepository();
            var fromDb = repository.GetDistinctYears();

            Assert.Greater(fromDb.Count, 10);
        }

        [Test]
        public void Can_get_Event_By_SubCategory()
        {
            IEventRepository repository = new EventRepository();
            var fromDb = repository.GetEventsBySubCategory("Running");

            Assert.Greater(fromDb.Count, 0);
        }
    }
}
